package com.lanou.dllo.soccernews.fragment.collectionFragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou.dllo.soccernews.R;
import com.lanou.dllo.soccernews.adapter.CollectionAdapter;
import com.lanou.dllo.soccernews.bean.CollectionBean;
import com.lanou.dllo.soccernews.bean.greendao.CollectionBeanArticlesEntity;
import com.lanou.dllo.soccernews.database.CollectionBeanDBhelp;
import com.lanou.dllo.soccernews.view.DividerItemDecoration;
import com.lanou.dllo.soccernews.volley.VolleySingleton;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 15/10/26.
 */
public class CollectionFragment extends Fragment {
    private RecyclerView collectRecycleview;
    private CollectionAdapter collectionwAdapte;
    private VolleySingleton volleySingleton;
    private CollectionBeanDBhelp help;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_circle, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        collectRecycleview = (RecyclerView) view.findViewById(R.id.circle_rcview);
        LinearLayoutManager lm = new LinearLayoutManager(getContext());
        lm.setOrientation(LinearLayoutManager.VERTICAL);
        collectRecycleview.addItemDecoration(new
                DividerItemDecoration(getContext(), LinearLayoutManager.VERTICAL));
        collectRecycleview.setLayoutManager(lm);
        collectionwAdapte = new CollectionAdapter();
        volleySingleton = VolleySingleton.getInstance();
        help = CollectionBeanDBhelp.getInstance();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        collectRecycleview.setAdapter(collectionwAdapte);
        //加载数据
        String url = "https://api.dongqiudi.com/tabs_static/2/iphone/11.json";
        List<CollectionBeanArticlesEntity> articlesEntities = help.getCollectionBeanArticlesEntity();
        if (articlesEntities != null && articlesEntities.size() > 0) {
            collectionwAdapte.addData(articlesEntities);
        } else {
            getFromIntenetData(url);
        }
    }

    private void getFromIntenetData(String url) {
        StringRequest request = new StringRequest(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Gson gson = new Gson();
                        CollectionBean collectionBean = gson.fromJson(response, CollectionBean.class);
                        if (collectionBean != null) {
                            List<CollectionBeanArticlesEntity> articles = collectionBean.getArticles();


                            if (articles != null && articles.size() > 0) {
                                collectionwAdapte.addData(articles);
                                help.addCollectionBeanArticlesEntity(articles);
                            }

                        }

                    }
                }, new Response.ErrorListener() {


            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                // TODO Auto-generated method stub
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");
                return map;
            }
        };
        volleySingleton.addRequest(request);
    }


}

